//
//  CustomSwitchView1.swift
//  MoyoungCommon
//
//  Created by 易先生 on 2022/10/25.
//

import UIKit
import RxSwift
import RxCocoa

public class CustomSwitchView: UIControl {
        
    public var onTintColor : UIColor? = R.color.common_27_main_color()
    
    public var thumbTintColor : UIColor? = R.color.common_30_switch_close()
    
    public var isOn: Bool = false
    
    public var onImage: UIImage? = R.image.ic_home_switch_open()
    
    public var offImage: UIImage? = R.image.ic_home_switch_close()

    public var barSize: CGSize = CGSize(width: 28, height: 28)
    
    public var xoffSetSpace: CGFloat = 2
    public var yoffSetSpace: CGFloat = 2
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.size = CGSize(width: 56, height: 32)
        self.addTarget(self, action: #selector(translateAnimate), for: .touchUpInside)
        makeUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        self.addTarget(self, action: #selector(translateAnimate), for: .touchUpInside)
        makeUI()
    }
    
    private func makeUI() {
        
        self.backgroundColor = R.color.common_30_switch_close()
        self.addSubview(iconImageView)
        iconImageView.isUserInteractionEnabled = false
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.layer.cornerRadius = self.bounds.size.height/2
        self.layer.masksToBounds = true
    }
    
    public func setOn(isOn: Bool) {
        if self.isOn != isOn {
            self.isOn = isOn
            reloadSwitchUI(isAnimate: false)
        }
    }

    @objc private func translateAnimate() {
        
        isOn = !isOn
        reloadSwitchUI(isAnimate: true)
        self.sendActions(for: UIControl.Event.valueChanged)
        HapticFeedback.Impact.light()
    }
    
    private func reloadSwitchUI(isAnimate: Bool) {
        self.backgroundColor = isOn ? onTintColor : thumbTintColor
        iconImageView.image = isOn ? onImage : offImage
        
        if isOn {
            if isAnimate {
                UIView.animate(withDuration: 0.3) {
                    self.iconImageView.left = self.bounds.size.width - (self.barSize.width+self.xoffSetSpace)
                }
            } else {
                self.iconImageView.left = self.bounds.size.width - (self.barSize.width+self.xoffSetSpace)
            }
            
        } else {
            if isAnimate {
                UIView.animate(withDuration: 0.3) {
                    self.iconImageView.left = self.xoffSetSpace
                }
            } else {
                self.iconImageView.left = self.xoffSetSpace
            }
            
        }
    }
    
    private lazy var iconImageView: UIImageView = {
        let iconImageView = UIImageView(frame: CGRect(x: xoffSetSpace, y: yoffSetSpace, width: barSize.width, height: barSize.height))
        iconImageView.image = offImage
        return iconImageView
    }()

}

extension Reactive where Base: CustomSwitchView {

    public var isOn: ControlProperty<Bool> {
        value
    }
    
    public var value: ControlProperty<Bool> {
        return base.rx.controlProperty(editingEvents: .touchUpInside) { uiSwitch in
            uiSwitch.isOn
        } setter: { uiSwitch, value in
            uiSwitch.setOn(isOn: value)
        }
    }
}
